﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;

namespace Domzs.Extensions
{
    public static class SerializeExtension
    {
        /*
           int value = 12;
           string xml = ObjectSerializeToXmlString(value);
           value = ObjectSerializeFromXmlString<Int32>(xml);
           Console.WriteLine(value);
        */

        public static T ObjectSerializeFromXmlString<T>(string xml)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(T));

            using (StringReader reader = new StringReader(xml))
            {
                return (T)serializer.Deserialize(reader);
            }
        }

        public static string ObjectSerializeToXmlString(object obj)
        {
            XmlSerializer serializer = new XmlSerializer(obj.GetType());

            using (StringWriter writer = new StringWriter())
            {
                serializer.Serialize(writer, obj);

                return writer.ToString();
            }
        }
    }
}
